<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\Controller;
use App\Models\client\BannerModel;
use App\Models\client\MsgModel;
use App\Models\client\OpenInfoModel;
use App\Models\CustomerModel;
use App\Models\OrderModel;
use App\Models\RecBillModel;
use App\Models\service\ProblemModel;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;


class Dashboard extends Controller
{

    public function index(Request $request)
    {
        $auth = $request->get('auth');
        $customer = CustomerModel::where('id', $auth->id)->first();

        $return = [];
        $return['avatar'] = $customer->avatar ?? '';

        //banner
        $banners = BannerModel::where('status', 1)->select('id', 'title', 'picture_url', 'link_url')->get();
        if($banners){
            $return['banners'] = $banners->toArray();
        }else{
            $return['banners'] = [];
        }
        //通知消息
        $msg = MsgModel::whereRaw('FIND_IN_SET(?, customer_ids)', [$auth->id])->select('id', 'title', 'created_at')->orderBy('id', 'desc')->get();
        if($msg){
            $return['msg'] = $msg->map(function($item) {
                $res = [
                    'id' => $item->id,
                    'title' => $item->title,
                    'created_at' => $item->created_at->toDateTimeString(),
                ];
                return $res;
            })->toArray();
        }else{
            $return['msg'] = [];
        }

        //图表
        $date_week = date('Y-m-d', strtotime('-6 day'));
        $orders_pre = OrderModel::where('customer_id', $auth->id)->where('pre_time', '>=', $date_week)->orderBy('pre_time', 'asc')->get();
        $orders_storage = OrderModel::where('customer_id', $auth->id)->where('storage_time', '>=', $date_week)->orderBy('storage_time', 'asc')->get();
        $orders_out = OrderModel::where('customer_id', $auth->id)->where('outstock_time', '>=', $date_week)->orderBy('outstock_time', 'asc')->get();
        $orders_arrive = OrderModel::where('customer_id', $auth->id)->where('arrive_time', '>=', $date_week)->orderBy('arrive_time', 'asc')->get();

        $order_chart = [];
        for($i = 6; $i >= 0; $i--){
            $date = date('Y-m-d', strtotime('-'.$i.' day'));

            $pre_count = $orders_pre->filter(function($item) use ($date) {
                return date('Y-m-d', strtotime($item->pre_time)) == $date;
            })->count();

            $storage_count = $orders_storage->filter(function($item) use ($date) {
                return date('Y-m-d', strtotime($item->storage_time)) == $date;
            })->count();

            $out_count = $orders_out->filter(function($item) use ($date) {
                return date('Y-m-d', strtotime($item->outstock_time)) == $date;
            })->count();

            $arrive_count = $orders_arrive->filter(function($item) use ($date) {
                return date('Y-m-d', strtotime($item->arrive_time)) == $date;
            })->count();

            $order_chart[] = [
                'date' => $date,
                'pre' => $pre_count,
                'storage' => $storage_count,
                'out' => $out_count,
                'arrive' => $arrive_count,
            ];

        }
        $return['order_chart'] = $order_chart;

        //账户信息
        $customer = CustomerModel::where('id', $auth->id)->first();
        $customer_info = [
            'name' => $customer->name,
            'phone' => $customer->contact_phone,
        ];
        $return['customer_info'] = $customer_info;

        //下单信息
        $orders = OrderModel::where('customer_id', $auth->id)->get();
        $order_info = [
            'total_count' => $orders->count(),
            'total_pre' => $orders->filter(function($item) {
                return $item->status == OrderModel::STATUS_PREORDER;
            })->count(),
            'total_confirm' => $orders->filter(function($item) {
                return $item->status == OrderModel::STATUS_CONFIRM;
            })->count(),
            'total_storage' => $orders->filter(function($item) {
                return $item->status == OrderModel::STATUS_STORAGE;
            })->count(),
//            'total_out' => $orders->filter(function($item) {
//                return $item->status == OrderModel::STATUS_OUTSTOCK;
//            })->count(),
            'total_arrive' => $orders->filter(function($item) {
                return $item->status == OrderModel::STATUS_ARRIVE;
            })->count(),
        ];


        $bill_confirm = RecBillModel::where('customer_id', $auth->id)->where('status', RecBillModel::BILL_CONFIRMED)->count();

        $total_take_order = TakeOrderModel::where('customer_id', $auth->id)->count();

        $total_problem = ProblemModel::where('customer_id', $auth->id)->where('status', 1)->count();

        $platform_info = OpenInfoModel::select('mobile', 'email', 'wechat')->first()->toArray();

        $return = array_merge($return, [
            'order_info' => $order_info,
            'bill_confirm' => $bill_confirm,
            'total_take_order' => $total_take_order,
            'total_problem' => $total_problem,
            'platform_info' => $platform_info,
        ]);

        return $this->success($return);

    }


}
